﻿/*
 * Created by SharpDevelop.
 * User: alca7raz
 * Date: 11/15/2010
 * Time: 8:12 PM
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Collections.Generic;
using System.Xml;

namespace Echium2
{
	/// <summary>
	/// Description of EchiumLibrary.
	/// </summary>
	public class EchiumLibrary
	{
		public string Filename
		{
			get; private set;
		}
		
		public Dictionary<string, EchiumPlaylist> Playlists
		{
			get; private set;
		}
		
		public Dictionary<int, EchiumTrack> Tracks
		{
			get; private set;
		}
		
		public EchiumLibrary()
		{
			this.Playlists = new Dictionary<string, EchiumPlaylist>();
			this.Tracks = new  Dictionary<int, EchiumTrack>();
		}
		
		public EchiumPlaylist NewPlaylist(string name)
		{
			EchiumPlaylist playlist = new EchiumPlaylist(this, name);
			this.Playlists.Add(name, playlist);
			return Playlists[name];
		}
		
		public EchiumTrack NewTrack(string Title, string Location, long Duration, int ID)
		{
			EchiumTrack track = new EchiumTrack(this, Title, Location, Duration, ID);
			this.Tracks.Add(ID, track);
			return Tracks[ID];
		}
		
		public EchiumTrack NewTrack(string Title, string Location, long Duration)
		{
			int ID = 0;
			if (this.Tracks.Count > 0) ID = Tracks[Tracks.Count-1].ID+1;
			EchiumTrack track = new EchiumTrack(this, Title, Location, Duration, ID);
			this.Tracks.Add(ID, track);
			return Tracks[ID];
		}
		
		public void LoadLibrary(string filename)
		{
			this.Filename = filename;
			XmlDocument doc = new XmlDocument();
			doc.Load(filename);
			foreach (XmlNode trackNode in doc.GetElementsByTagName("Track"))
			{
				XmlElement trackElement = (XmlElement)trackNode;
				string trackTitle = trackElement.GetElementsByTagName("Title")[0].InnerText;
				string trackLocation = trackElement.GetElementsByTagName("Location")[0].InnerText;
				long trackDuration = long.Parse(trackElement.GetElementsByTagName("Duration")[0].InnerText);
				int trackID = int.Parse(trackElement.GetElementsByTagName("ID")[0].InnerText);
				EchiumTrack track = this.NewTrack(trackTitle, trackLocation, trackDuration, trackID);
				if (trackElement.GetElementsByTagName("Artist").Count > 0)
					track.Artist = trackElement.GetElementsByTagName("Artist")[0].InnerText;
				if (trackElement.GetElementsByTagName("Album").Count > 0)
					track.Artist = trackElement.GetElementsByTagName("Album")[0].InnerText;
			}
			foreach (XmlNode playlistNode in doc.GetElementsByTagName("Playlist"))
			{
				XmlElement playlistElement = (XmlElement)playlistNode;
				string playlistName = playlistElement.GetElementsByTagName("Name")[0].InnerText;
				EchiumPlaylist playlist = this.NewPlaylist(playlistName);
				foreach (XmlNode playlistTrackNode in playlistElement.GetElementsByTagName("PlaylistID"))
				{
					playlist.TrackIDs.Add(int.Parse(playlistTrackNode.InnerText));
				}
			}
		}
		
		public void SaveLibrary()
		{
			if (!String.IsNullOrEmpty(this.Filename)) SaveLibrary(this.Filename);
		}
		
		public void SaveLibrary(string filename)
		{
			XmlDocument doc = new XmlDocument();
			XmlElement root = doc.CreateElement("Library");
			XmlElement tracks = doc.CreateElement("Tracks");
			foreach (EchiumTrack track in this.Tracks.Values)
			{
				XmlElement trackNode = doc.CreateElement("Track");
				XmlElement titleNode = doc.CreateElement("Title");
				titleNode.InnerText = track.Title;
				trackNode.AppendChild(titleNode);
				XmlElement locationNode = doc.CreateElement("Location");
				locationNode.InnerText = track.Location;
				trackNode.AppendChild(locationNode);
				XmlElement durationeNode = doc.CreateElement("Duration");
				durationeNode.InnerText = track.Duration.ToString();
				trackNode.AppendChild(durationeNode);
				XmlElement idNode = doc.CreateElement("ID");
				idNode.InnerText = track.ID.ToString();
				trackNode.AppendChild(idNode);
				if (!string.IsNullOrEmpty(track.Artist))
				{
					XmlElement artistNode = doc.CreateElement("Artist");
					artistNode.InnerText = track.Artist;
					trackNode.AppendChild(artistNode);
				}
				if (!string.IsNullOrEmpty(track.Album))
				{
					XmlElement albumNode = doc.CreateElement("Album");
					albumNode.InnerText = track.Album;
					trackNode.AppendChild(albumNode);
				}
				tracks.AppendChild(trackNode);
			}
			root.AppendChild(tracks);
			XmlElement playlists = doc.CreateElement("Playlists");
			foreach (EchiumPlaylist playlist in Playlists.Values)
			{
				XmlElement playlistNode = doc.CreateElement("Playlist");
				XmlElement nameNode = doc.CreateElement("Name");
				nameNode.InnerText = playlist.PlaylistName;
				playlistNode.AppendChild(nameNode);
				XmlElement playlistIDs = doc.CreateElement("PlaylistIDs");
				foreach (int ID in playlist.TrackIDs)
				{
					XmlElement idNode = doc.CreateElement("PlaylistID");
					idNode.InnerText = ID.ToString();
					playlistIDs.AppendChild(idNode);
				}
				playlistNode.AppendChild(playlistIDs);
			}
			root.AppendChild(playlists);
			doc.AppendChild(root);
			doc.Save(filename);
		}
	}
}
